package com.wang.vedio.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.luck.picture.lib.tools.ScreenUtils;
import com.vondear.rxtool.view.RxToast;
import com.wang.vedio.R;

import java.util.ArrayList;
import java.util.List;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

public class TabScrollView extends ScrollView {

	private Context mContext;
	private LinearLayout parentLinearLayout;
	private List<String> titles = new ArrayList<String>();
	private int scrollHeight = ScreenUtils.getScreenHeight(getContext()) / 6;
	private int lineColor = R.color.tab_menu_text_select;
	private float titleTextSize;
	private int selectPosition = 0;
	private int selectTextColor;
	private int unSelectTextColor;
	private int selectBgIcon;
	private int unSelectBgIcon;
	private MyRelativeLayout selectTextView;
	private int screenWidth = ScreenUtils.getScreenWidth(getContext());
	private int screenHeight = ScreenUtils.getScreenHeight(getContext());
	
	private TypeClickListener mTypeClickListener = null;
	
	public void setTypeClickListener(TypeClickListener l){
		this.mTypeClickListener = l;
	}

	public TabScrollView(Context context) {
		this(context, null);
	}

	public TabScrollView(Context context, AttributeSet attrs) {
		this(context, attrs, -1);
	}

	public TabScrollView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		parentLinearLayout = new LinearLayout(context);
		parentLinearLayout.setOrientation(LinearLayout.VERTICAL);
		addView(parentLinearLayout, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
		this.mContext = context;
	}

	/**
	 * @说明 设置titles(这里用一句话描述这个方法的作用)

	 * @param titles
	 */
	public TabScrollView setTitles(List<String> titles) {
		this.titles = titles;
		return this;
	}
	
	public TabScrollView setTabScrollHeight(int tabScrollHeight) {
		this.scrollHeight = tabScrollHeight;
		return this;
	}

	/**
	 * @说明 设置选中的菜单的颜色(这里用一句话描述这个方法的作用)

	 * @param selectTextColor
	 */
	public TabScrollView setSelectTextColor(int selectTextColor) {
		this.selectTextColor = selectTextColor;
		return this;
	}

	/**
	 * @说明 设置不选中的菜单的颜色(这里用一句话描述这个方法的作用)

	 * @param unSelectTextColor
	 */
	public TabScrollView setUnSelectTextColor(int unSelectTextColor) {
		this.unSelectTextColor = unSelectTextColor;
		return this;
	}

	/**
	 * @说明 设置选中的背景图片(这里用一句话描述这个方法的作用)

	 * @param selectBgIcon
	 */
	public TabScrollView setSelectBgIcon(int selectBgIcon) {
		this.selectBgIcon = selectBgIcon;
		return this;
	}

	/**
	 * @说明 设置没有选中的背景图片(这里用一句话描述这个方法的作用)

	 * @param unSelectBgIcon
	 */
	public TabScrollView setUnSelectBgIcon(int unSelectBgIcon) {
		this.unSelectBgIcon = unSelectBgIcon;
		return this;
	}

	public TabScrollView setTitleTextSize(int textSize) {
		this.titleTextSize = textSize;
		return this;
	}
	
	public TabScrollView setSelectPosition(int selectPosition) {
		this.selectPosition = selectPosition;
		return this;
	}

	public void createTab() {
		if (titles != null && titles.size() != 0) {
			for (int i = 0; i < titles.size(); i++) {
				addTextView(i, titles.get(i));
			}
		} else {
			RxToast.normal("请设置各种属性");
		}
	}

	private void addTextView(final int index, String title) {
		MyRelativeLayout myTextView = new MyRelativeLayout(mContext);
//		myTextView.setGravity(Gravity.CENTER);
		myTextView.setIndex(index);
//		myTextView.setTitle(title);
		myTextView.setTitle(title);
		myTextView.setBackgroundColor(mContext.getResources().getColor(R.color.white));
		if (titleTextSize != 0) {
			myTextView.setTitleSize(titleTextSize);
		}
		myTextView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				MyRelativeLayout textView = (MyRelativeLayout) v;
				if (selectTextView != null && textView.getIndex() != selectTextView.getIndex()) {
					selectTextView.getTextView().setTextColor(getResources().getColor(unSelectTextColor));
					if(selectBgIcon != 0 && selectBgIcon != -1){
						selectTextView.setBackgroundColor(getResources().getColor(unSelectBgIcon));
						selectTextView.getLeftView().setBackgroundColor(getResources().getColor(unSelectBgIcon));
						textView.setBackgroundResource(selectBgIcon);
						textView.getLeftView().setBackgroundColor(getResources().getColor(R.color.tab_menu_text_select));
					}
					textView.getTextView().setTextColor(getResources().getColor(selectTextColor));
					selectTextView = textView;
					int offx = textView.getTop() - (getHeight() - scrollHeight) / 2;
					if (offx < 0) {
						smoothScrollTo(0, 0);
					} else {
						smoothScrollTo(textView.getTop(), offx);
					}
					if (mTypeClickListener != null) {
						mTypeClickListener.typeClick(textView,index);
					}
				}
			}
		});
		if(selectPosition == index){
			myTextView.getTextView().setTextColor(getResources().getColor(selectTextColor));
			if (selectBgIcon != 0 && selectBgIcon != -1) {
				myTextView.setBackgroundColor(getResources().getColor(selectBgIcon));
				myTextView.getLeftView().setBackgroundColor(getResources().getColor(R.color.tab_menu_text_select));
			}
			selectTextView = myTextView;
			smoothScrollTo(0, 0);
		} else {
			myTextView.getTextView().setTextColor(getResources().getColor(unSelectTextColor));
			if (selectBgIcon != 0 && selectBgIcon != -1) {
				myTextView.setBackgroundColor(getResources().getColor(unSelectBgIcon));
				myTextView.getLeftView().setBackgroundColor(getResources().getColor(unSelectBgIcon));
			}
		}
		parentLinearLayout.addView(myTextView, new LinearLayout.LayoutParams(MATCH_PARENT, scrollHeight));
	}

	public class MyRelativeLayout extends RelativeLayout{

		private int index;
		private View leftView;
		private TextView tvContent;

		public  MyRelativeLayout(Context context){
			super(context);
			tvContent = new TextView(mContext);
			RelativeLayout.LayoutParams linearParams = new RelativeLayout.LayoutParams(ScreenUtils.dip2px(mContext,100), ScreenUtils.dip2px(mContext,45)); //取控件textView当前的布局参数
			tvContent.setLayoutParams(linearParams); //使设置好的布局参数应用到控件;
			tvContent.setGravity(Gravity.CENTER);
			tvContent.setLines(1);
			tvContent.setEllipsize(TextUtils.TruncateAt.END);
			tvContent.setPadding(ScreenUtils.dip2px(mContext,16),0,ScreenUtils.dip2px(mContext,16),0);
			addView(tvContent);

			leftView = new TextView(mContext);
			RelativeLayout.LayoutParams leftViewLp = new RelativeLayout.LayoutParams(ScreenUtils.dip2px(mContext,5), ScreenUtils.dip2px(mContext,15)); //取控件textView当前的布局参数
			leftViewLp.addRule(RelativeLayout.CENTER_VERTICAL);
			leftView.setLayoutParams(leftViewLp); //使设置好的布局参数应用到控件
			leftView.setBackgroundColor(getResources().getColor(R.color.tab_menu_text_select));
			addView(leftView);

		}

		public void setTitle(String title){
			this.tvContent.setText(title);
		}
		public void setTitleSize(float titleSize){
			this.tvContent.setTextSize(titleSize);
		}

		public TextView getTextView(){
			return tvContent;
		}

		public View getLeftView(){
			return leftView;
		}

		public int getIndex() {
			return index;
		}

		public void setIndex(int index) {
			this.index = index;
		}
	}

//	public class MyTextView extends AppCompatTextView {
//		private int index;
//
//		public MyTextView(Context context) {
//			super(context);
//			setLines(1);
//			setEllipsize(TextUtils.TruncateAt.END);
//			setTextSize(ScreenUtils.dip2px(context,16));
//			setPadding(28,5,5,5);
//		}
//
//		public int getIndex() {
//			return index;
//		}
//
//		public void setIndex(int index) {
//			this.index = index;
//		}
//
//	}

	public MyRelativeLayout getSelectTextView(){
		return selectTextView;
	}
	
	public interface TypeClickListener {
		void typeClick(MyRelativeLayout myTextView, int position);
	}

}
